//
//  RJCheckDetailVC.m
//  AirQuality
//
//  Created by Po on 15/4/7.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJCheckDetailVC.h"

@interface RJCheckDetailVC ()
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UILabel *positionLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (weak, nonatomic) IBOutlet UITextView *detailText;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UIButton *checkButton;

@property (retain, nonatomic) NSDictionary * data;

- (void)initInterface;
@end

@implementation RJCheckDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initInterface];
    [self.view setFrame:ScreenBounds];
}

- (void)initInterface
{
    [_detailText setEditable:NO];
    [_cancelButton.layer setBorderWidth:1];
    [_cancelButton.layer setBorderColor:GetRGBColor(207, 207, 207, 1).CGColor];
    [_checkButton.layer setBorderWidth:1];
    [_checkButton.layer setBorderColor:GetRGBColor(207, 207, 207, 1).CGColor];
    [_positionLabel setText:_data[@"stationName"]];
    [_timeLabel setText:_data[@"timepoint"]];
    [_typeLabel setText:_data[@"GasType"]];
    [_numberLabel setText:_data[@"value"]];
    [self setDEtailTextWithMark:_data[@"OldMark"]];
}

- (void)setDEtailTextWithMark:(NSString *)mark
{
    NSDictionary * keyValue = @{@"H":@"有效数据不足",
                                @"W":@"等待数据恢复",
                                @"BB":@"连接不良",
                                @"D":@"分析仪器离线",
                                @"B":@"运行不良",
                                @"R":@"数据突变",
                                @"r":@"数据不变",
                                @"PZ":@"零点检查",
                                @"PS":@"跨度检查",
                                @"AS":@"精度检查",
                                @"CZ":@"零点校准",
                                @"CS":@"跨度校准",
                                @"TZS":@"检定零点漂移",
                                @"TSS":@"检定跨度漂移",
                                @"TSL":@"检定多点跨度",
                                @"TSR":@"检定跨度重现性",
                                @"RM":@"无效数据",
                                @"ZO":@"已修正数据",
                                @"PSB":@"跨度检查不通过",
                                @"HSp":@"高值提示",
                                @"LSp":@"数据超下限",
                                @"OL":@"离群值",
                                @"DG":@"PM2.5与PM10倒挂",
                                @"FC":@"浮尘天气",
                                @"JG":@"秸秆燃烧",
                                @"ZWR":@"重污染天气",
                                @"ZK":@"质量控制"};
    
    NSMutableString * tempString = [mark mutableCopy];
    NSMutableString * detailString = [mark mutableCopy];
    BOOL isDown = NO;
    NSUInteger pointPosition = 0;
    while (!isDown) {
        
        //获取逗号
        pointPosition = [tempString rangeOfString:@","].location;
        
        //截取关键字
        NSString * keyString;
        if (pointPosition == NSNotFound) {
            keyString = tempString;
            NSString * string = keyValue[keyString];
            if (string) {
                detailString = [[detailString stringByReplacingOccurrencesOfString:keyString withString:keyValue[keyString]] mutableCopy];
            }
            isDown = YES;
        } else {
            keyString = [tempString substringToIndex:pointPosition];
            NSString * string = keyValue[keyString];
            if (string) {
                detailString = [[detailString stringByReplacingOccurrencesOfString:keyString withString:keyValue[keyString]] mutableCopy];
            }
            tempString = [[tempString substringFromIndex:(pointPosition + 1)] mutableCopy];
        }
    }
    [_detailText setText:detailString];
}

- (void)setData:(NSDictionary *)data
{
    _data = [NSDictionary dictionaryWithDictionary:data];
}

#pragma mark - event
- (IBAction)pressCancelButton:(id)sender {
    [self removeSelf];
}

- (IBAction)pressConfirmButton:(id)sender {
    if (_delegate) {
        [_delegate didSelect:self Button:1];
    }
    [self removeSelf];
}

- (void)removeSelf
{
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    if ((point.y < _mainView.frame.origin.y) || (point.y > CGRectGetMaxY(_mainView.frame))) {
        [self removeSelf];
    }
}
@end
